//
//  MarketViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/8/27.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "MarketViewController.h"
#import "BaseTableView.h"
#import "BaseView.h"
#import "BaseButton.h"
#import "DLMacro.h"
#import "BaseLabel.h"
#import "MarketTableViewCell.h"
#import "DLWNetworkingParser.h"
#import "Home_NewsViewControllerWeb.h"
#import "Inquire_ViewController.h"
#import "Offer_ViewController.h"
@interface MarketViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrData;
@end

@implementation MarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createSubview];
    [self handleeData];
}
#pragma mark - 获取数据
- (void)handleeData
{
    _arrData = [NSMutableArray array];
    [DLWNetworkingParser POSTWithURL:@"http://www.danjiguanjia.com/api/Price/getPrice" par:nil success:^(id responseObject) {
        [_arrData addObject: responseObject[@"data"][@"egg"]];
        [_arrData addObject: responseObject[@"data"][@"corn"]];
        [_arrData addObject: responseObject[@"data"][@"bean"]];
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}


- (void)createSubview
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:nil];
    
    _tableView = [[BaseTableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self tableViewHeaderView];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[MarketTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

#pragma mark - tableView的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MarketTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.lableClass.text = @"鸡蛋";
        cell.dic = _arrData[indexPath.row];
        return cell;
    } else if (indexPath.row == 1)
    {
        cell.lableClass.text = @"玉米";
        cell.dic = _arrData[indexPath.row];
        return cell;
    } else
    {
        cell.lableClass.text = @"豆粕";
        cell.dic = _arrData[indexPath.row];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

//创建分区的头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    BaseLabel *lable = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 50)];
    lable.backgroundColor = [UIColor orangeColor];
    lable.text = @"热门价格动态";
    lable.textColor = [UIColor whiteColor];
    lable.textAlignment = 1;
    return lable;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}


#pragma mark - 创建tableView的头视图
- (void)tableViewHeaderView
{
    NSArray *arrButton = @[@"2 dan jia",@"di tu",@"qu xian",@"2 yuan liao jia",@"2 yuan liao qu xian",@"2 cha xun"];
    NSArray *arrLabel = @[@"鸡蛋报价",@"蛋价地图",@"蛋价曲线",@"原料报价",@"原料曲线",@"查询统计"];
    
    BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 265)];
    view.backgroundColor = [UIColor whiteColor];
    _tableView.tableHeaderView = view;
    // 创建button
    for (NSInteger i = 0; i < 6; i++) {
        BaseButton *buttom = [BaseButton buttonWithType:UIButtonTypeCustom];
        buttom.frame = CGRectMake(VIEW_WIDTH * (i % 3) / 3.0  + 25, 10 + i / 3 * 130, (VIEW_WIDTH - 150) / 3.0, 80);
        [buttom setBackgroundImage:[UIImage imageNamed:arrButton[i]] forState:UIControlStateNormal];
        buttom.tag = 100 + i;
        // button的点击事件
        [buttom addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:buttom];
    }
    // 创建lable
    for (NSInteger i = 0; i < 6; i++) {
        BaseLabel *lable = [[BaseLabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH * (i % 3) / 3.0 + 25, 90 + i / 3 * 130, (VIEW_WIDTH - 150) / 3.0, 30)];
        lable.text = arrLabel[i];
        lable.font = [UIFont systemFontOfSize:13];
        [view addSubview:lable];
    }
    
    
}

#pragma mark - button 的点击事件
- (void)buttonAction:(BaseButton *)button
{

    switch (button.tag) {
        case 100:
        {
            Offer_ViewController *offerVC = [[Offer_ViewController alloc] init];
            offerVC.navigationTitle = @"鸡蛋报价";
            [self.navigationController pushViewController:offerVC animated:YES];
        }
            break;
        case 101:
        {
            // 这个时候需要加 大括号,,要不创建不了对象.
            Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
           webVC.name = @"map";
            webVC.navigationTitle = @"蛋价地图";
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        case 102:
        {
            Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
            webVC.name = @"price";
            webVC.navigationTitle = @"蛋价曲线";
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        case 103:
        {
            Offer_ViewController *offerVC = [[Offer_ViewController alloc] init];
            offerVC.navigationTitle = @"原料报价";
            [self.navigationController pushViewController:offerVC animated:YES];
        }
            break;
        case 104:
        {
            Home_NewsViewControllerWeb *webVC = [[Home_NewsViewControllerWeb alloc] init];
            webVC.name = @"raw";
            webVC.navigationTitle = @"原料曲线";
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        case 105:
        {
            Inquire_ViewController *inquireVC = [[Inquire_ViewController alloc] init];
            [self.navigationController pushViewController:inquireVC animated:YES];
        }
            break;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
